/** @file    LogHandlerQRenderer.cpp
 *  @author  Alessandro Polo
 *  @version $Id: LogHandlerQRenderer.cpp 2704 2010-06-24 01:08:48Z alex $
 *  @brief
 * File containing methods for the wosh::gui::LogHandlerQRenderer class.
 * The header for this class can be found in LogHandlerQRenderer.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include <framework/gui/components/LogHandlerQRenderer.h>

 #include <core/ObjectAllocator.h>


using namespace std;
namespace wosh {
 extern const char* LogHandlerPlainText__LogLevelStr[];

 namespace gui {

 WOSH_REGISTER(wosh::gui::LogHandlerQRenderer, "wosh::LogHandler", 1.01, _static_LogHandlerQRenderer)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT LogHandlerQRenderer::open() {
	MutexLockerWrite mL(&this->configMux);
	this->buffer = new char[2047];
	return WRET_OK;
 }

WRESULT LogHandlerQRenderer::close() {
	MutexLockerWrite mL(&this->configMux);
	delete [] this->buffer; this->buffer = NULL;
	return WRET_OK;
 }

bool LogHandlerQRenderer::isOpen() const {
	MutexLockerRead mL(&this->configMux);
	return true;
 }
/*

<p> LEVEL-TIME CONTEXT MESSAGE</p>

*/

const char* LogHandlerQRenderer__LogLevelColor[] = {
	"black",	///< (LogLevel)0 UNKNOWN
	"black",	///< (LogLevel)1 NONE
	"red",		///< (LogLevel)2 CRITICAL
	"orange",	///< (LogLevel)3 WARNING
	"blue",		///< (LogLevel)4 INFO
	"gray",		///< (LogLevel)5 VERBOSE
	"gray",		///< (LogLevel)6 DEBUG
	"black"		///< (LogLevel)7 ALL
 };

// really-multithread safe
void LogHandlerQRenderer::log( const char* context, LogLevel level, const char* format, va_list args )
 {
	this->configMux.lockForRead();
	this->writeMux.lock();

	vsprintf(this->buffer, format, args );
	QString buf(this->buffer);
	//buf.vsprintf(format, args);
	if ( context != NULL ) {
		if ( this->printColoured ) {
			int i = -1;
			int len = strlen(context);
			while ( ++i < len  ) {
				if ( context[i] != ' ')
					break;
			 }
			buf.prepend(context+i);
			while ( i-- > 0 ) buf.prepend("&nbsp;");
		 }
		else {
			buf.prepend(context);
		 }
	 }
	bufferTime( "%H:%M:%S" );
	buf.prepend(this->bufferDateTime);
	buf.prepend(LogHandlerPlainText__LogLevelStr[level]);
	if ( this->printColoured ) {
		buf.prepend( QString("<font color=\"%1\">").arg(LogHandlerQRenderer__LogLevelColor[level]));
		buf.append("</font><br/>");
	 }
	//buf.append("\r\n");

	emit appendLine( buf );

	this->writeMux.unLock();
	this->configMux.unLock();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void LogHandlerQRenderer::setColoured( bool enabled ) {
	MutexLockerWrite mL(&this->configMux);
	this->printColoured = enabled;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
